function [scaled_ATT_bs,ATE_bs,W_bs_Ghat] = ewm_rct_test_cubic_input(D,Y,ps,beta,X,in_Ghat,g,Yscale,bsperm,n) 
% This function calculates the scaled ATT and ATE, as well as the EWM point
% estimates on the bootstrap sample 

% Inputs: 
% (1) D: original opower treatment status 
% (2) Y: original outcome 
% (3) ps: original propensity score
% (4) beta: treatment rule parameter for the optimal rule on the original sample  
% (5) X : original covariates
% (6) in_Ghat: optimal rule on the original sample  
% (7) g: g on original sample
% (8) Yscale: scaling factor for the original sample
% (9) bsperm: bootstrap index 
% (10) n: number of obs

% Outputs: 
% (1) scaled_ATT_bs: scaled ATT on the bootstrap sample
% (2) ATE_bs: ATE on the bootstrap sample 
% (3) W_bs_Ghat: EWM point estimate by applying beta parameters on the
% bootstrap sample
%% calculate scaled ATT and ATE for each bootstrap sample 
scaled_ATT_bs = (sum(D(bsperm,:))/n).*...
        (sum(D(bsperm,:).*Y(bsperm,:)-((1-D(bsperm,:)).*Y(bsperm,:).*ps(bsperm,:))./(1-ps(bsperm,:)))/sum(D(bsperm,:))).*Yscale;
ATE_bs = mean(g(bsperm)).*Yscale; % equiv. to ATE = mean(D.*Y./ps)-mean(((1-D).*Y)./(1-ps));

%% apply G_hat to bootstrap sample 
in_Ghat_in_bs_space=(X(bsperm,:)*beta>0);
W_bs_Ghat=sum(in_Ghat_in_bs_space.*g(bsperm))/n.*Yscale;